/*
 * The copyrights of this file belong to purple flower.

 * purple flower All rights reserved.
 *
 * No part of this document may be reproduced or transmitted in any form or by
 * any means, electronic or mechanical, including photocopying and recording,
 * for any purpose without prior written permission of purple flower
 *
 * This software contains a module which is based on the work of OpenIntents.
 * OpenIntents is published under "Apache License 2", the same license under
 * which Android is released. This will provide commercial and non-commercial 
 * third-party developers the same options and possibilities as Android itself.
 * If you want to use those codes, please connect to me (purpleflower912@gmail.com)
 */
package com.spk.quake2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.spk.quake2.R;

public class SetupActivity extends Activity {

	private DownloadTask mDownloadTask = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(R.string.setup_title);
		setContentView(R.layout.tools);


		CharSequence[] mirrors;

		Resources res = getResources();
		mirrors = res.getTextArray(R.array.mirrors);
		
		Spinner s = (Spinner) findViewById(R.id.spinner);
		ArrayAdapter adapter = ArrayAdapter.createFromResource(
				this, R.array.mirrors, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);

		Button button_download = (Button)this.findViewById(R.id.button_download);
		button_download.setOnClickListener( new OnClickListener() {  
			@Override  
			public void onClick(View v) {
				 
				 Spinner s = (Spinner) findViewById(R.id.spinner);
				 CharSequence url = (CharSequence)s.getSelectedItem();
				 
				 Log.i( "Quake2", "url= " + url);

				 mDownloadTask = new DownloadTask();
				 mDownloadTask.set_context(SetupActivity.this, url.toString());
				 mDownloadTask.execute();
			 }  
		}); 
		
		final Context context = this;
		Button button_setup = (Button)this.findViewById(R.id.button_setup);
		button_setup.setOnClickListener( new OnClickListener() {  
			@Override  
			public void onClick(View v) {
				 Spinner s = (Spinner) findViewById(R.id.spinner);
				 CharSequence url = (CharSequence)s.getSelectedItem();				
				 SetupTask setup = new SetupTask();

				 setup.set_context(SetupActivity.this, url.toString());
				 setup.execute();
			 }
		});
		
		Button button_config = (Button)this.findViewById(R.id.button_config);
		button_config.setOnClickListener( new OnClickListener() {  
			 @Override  
			 public void onClick(View v) {  
				copy_asset( true, "config.cfg" );
				copy_asset( true, "overlay1.tga" );
				copy_asset( true, "overlay2.tga" );
				copy_asset( true, "overlay3.tga" );
				
				Toast.makeText( context, "configuration is restored completely",  Toast.LENGTH_SHORT)
						.show();
			 }
		});
	}
	
    public void copy_asset( boolean overwrite, String name )
    {
    	String game_folder = "/sdcard/quake2";
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			File sdCardDir = Environment.getExternalStorageDirectory();
			if (sdCardDir != null) {
				game_folder = sdCardDir.getPath() + "/quake2";
			}
		}
		
    	if (overwrite ||
    		!(new File(game_folder + "/" + name)).exists()){
    		copy_asset( name, game_folder + "/" + name);
    	}
    }
    
    public static void copy_stream( InputStream in, OutputStream out )
    throws IOException
    {
    	byte[] buf = new byte[1024];    	
    	while(true){
    		int count = in.read(buf);
    		if (count<=0) break;
			out.write(buf, 0, count);
    	}
    }
    
    public void copy_asset( String name_in, String name_out )
    {
    	Log.i( "Quake2", String.format("copy_asset %s to %s",
    			name_in, name_out));
    	
    	AssetManager assets = this.getAssets();
    	
		try {
			InputStream in = assets.open(name_in);
			OutputStream out = new FileOutputStream(name_out);
			
			copy_stream(in, out);			
	    	
	    	out.close();
	    	in.close();
	    	
		} catch (Exception e) {

			e.printStackTrace();
		}
    }
}
